<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="<?php echo $URL_BASE ?>/static/css/index.css">

<?php
//include("app/views/partials/header.php");
?>

<title>Service de Laveries - INSA de Lyon</title>

<!-- Código en JavaScript -->
    
<script type="text/javascript">
    
	var tabla; 
	var tbl; 
	var tblBody; 
	var row; 
	var cell; 
	
	var numCol; 
	var numRow;
	var controlTable;  
	var stateTable;
	var color; 
	
	var end = ":00"; 
	var cont; 
	var conl; 
	
	var heure;
	var heure1;  
	var machine;	
	

	function createTable() {
		
		// get the reference for the body
        tabla = document.getElementById("tabla");
		
		// creates a <table> element and a <tbody> element
        tbl     = document.createElement("table");
        tblBody = document.createElement("tbody");
        
        // creating all cells
        for (var j = 0; j < numRow; j++) {
            // creates a table row
            row = document.createElement("tr");

            for (var i = 0; i < numCol; i++) {
				
				color = controlTable[j][i]; 
				cell = document.createElement("td");
				
				if(j==0 &&i>0){
					var cellText = document.createElement('span');
					cellText.innerHTML = ("Machine: " + conl)+'</ br>  <a href="#" onclick="reportFailure('+(i)+');"><img src="<?php echo $URL_BASE ?>/static/images/broken.gif"></a>';
					color = 4;
					conl++;				
				}
				else if(i==0 && j>0){
					var cellText = document.createTextNode(cont+ ":00 - " + (cont+1)+ ":00");
					cont++; 
				} else if(i==0 && j ==0){
					var cellText = document.createTextNode("");
					color = 0;
				}
				else{
					//var cellText = document.createTextNode('');
					if( controlTable[j][i] == 0){ // Machine free for booking
                		var cellText = document.createElement('span');
                		cellText.innerHTML = '<center><a href="#" onclick="book('+(i)+','+(j)+');"><img src="<?php echo $URL_BASE ?>/static/images/add.gif"></a></center>';
                	} else if( controlTable[j][i] == 3){ // Occupied by us
                		var cellText = document.createElement('span');
                		cellText.innerHTML = '<center><a href="#" onclick="cancel('+(i)+','+(j)+');"><img src="<?php echo $URL_BASE ?>/static/images/cancel.gif"></a></center>';
                	} else if( controlTable[j][i] == 2){ // Broken
                		var cellText = document.createElement('span');
                		cellText.innerHTML = '<center><img src="<?php echo $URL_BASE ?>/static/images/repairing.gif"></center>';
                	}
                	else { // Occupied by someone
                		var cellText = document.createElement('span');
                		cellText.innerHTML = '<center><img src="<?php echo $URL_BASE ?>/static/images/occupied.gif"></center>';
                	}
				}
				
				cell.appendChild(cellText);
				
				// Background Colors
				
				switch (color){
					
					case 0: 
						cell.bgColor = "white";
					break; 
					case 1:
					 	cell.bgColor = "white";
					break; 
					case 2:
					 	cell.bgColor = "lightgray";
					break; 
					case 3:
					 	cell.bgColor = "white";
					break; 
					case 4:
					 	cell.bgColor = "7ad8fa";
					break; 
					case 5:
					 	cell.bgColor = "blue";
					break; 
				}
				
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        tabla.appendChild(tbl);
        // sets the border attribute of tbl to 2;
        tbl.setAttribute("border", "2");  
    }
	
    function book(machine, hour){
    	var params = new Array();
    	params["machine"] = machine;
    	params["year"] = <?php echo($params['year']) ?>;
    	params["month"] = <?php echo($params['month']) ?>;
    	params["day"] = <?php echo($params['day']) ?>;
    	params["hour"] = hour;
    	params["residence"] = "<?php echo($params['residence']) ?>";

    	post_to_url("<?php echo $URL_BASE?>/user/addBooking", params, "post");
    }

    function cancel(machine, hour){
    	var params = new Array();
    	params["machine"] = machine;
    	params["year"] = <?php echo($params['year']) ?>;
    	params["month"] = <?php echo($params['month']) ?>;
    	params["day"] = <?php echo($params['day']) ?>;
    	params["hour"] = hour;
    	params["residence"] = "<?php echo($params['residence']) ?>";

    	post_to_url("<?php echo $URL_BASE?>/user/cancelBooking", params, "post");
    }

    function reportFailure(machine){
    	var params = new Array();
    	params["machine"] = machine;
    	params["residence"] = "<?php echo($params['residence']) ?>";

    	post_to_url("<?php echo $URL_BASE?>/user/reportFailure", params, "post");
    }

	function erase(){
		
		tabla.removeChild(tbl) 
		tbl = null; 
		return false; 
	}
	
	function setUp(){
		var i; 
		var j; 
		
		/*PHP var_dump :
		<?php var_dump($params); ?>*/

		setHeaderValues();
		setFormVisibility();

		numCol = <?php echo($params['nbMachine']); ?>; 
		numCol++; 
		numRow = 18; 
		cont = 7;  
		conl = 1; 
		
		// Inicialización de estados // 
		stateTable = new Array();
		controlTable = new Array(); 
			for(i=0; i<numRow; i++)
   			controlTable[i] = new Array();
			
		//  Background for machines
			
			for(j=0; j<numCol; j++)
      		controlTable[0][j] = 3;
			
		// Background for time
			
			for(j=1; j<numRow; j++)
      		controlTable[j][0] = 4;
			
			
		// Initialisation of machine states
			<?php
			for($j=1; $j < $params['nbMachine']+1; $j++){
				echo(sprintf("stateTable[%d] = %d; \n", $j, $params['availability'][$j-1]['isReported']));
				for($i=1; $i < 18; $i++){
					echo(sprintf("controlTable[%d][%d] = %d; \n", $i, $j, $params['availability'][$j-1][$i]));
				}
			}
			?>
		createTable(); 
	}
	
	function post_to_url(path, params, method) {
	    method = method || "post"; // Set method to post by default, if not specified.

	    // The rest of this code assumes you are not using a library.
	    // It can be made less wordy if you use one.
	    var form = document.createElement("form");
	    form.setAttribute("method", method);
	    form.setAttribute("action", path);

	    for(var key in params) {
	        if(params.hasOwnProperty(key)) {
	            var hiddenField = document.createElement("input");
	            hiddenField.setAttribute("type", "hidden");
	            hiddenField.setAttribute("name", key);
	            hiddenField.setAttribute("value", params[key]);

	            form.appendChild(hiddenField);
	         }
	    }

	    document.body.appendChild(form);
	    form.submit();
	}

	function setHeaderValues(){
		//Residence
		document.getElementById('residence').value = "<?php echo($params['residence']); ?>";
		//Day
		document.getElementById('day').value = "<?php echo($params['day']); ?>";
		//Month
		document.getElementById('month').value = "<?php echo($params['month']); ?>";
		//Year	
		document.getElementById('year').value = "<?php echo($params['year']); ?>";
	}

	function setFormVisibility(){
		if("<?php echo($params['username']) ?>" != ""){
			document.getElementById("notLoggedIn").style.visibility="hidden";
		} else {
			document.getElementById("loggedIn").style.visibility="hidden";
		}
	}

	function cancelAll(){
		var params = new Array();
		post_to_url("<?php echo $URL_BASE?>/user/cancelAll", params, "post");
	}

	function refreshIndex(){
    	var params = new Array();

    	params["residence"] = document.getElementById('residence').options[document.getElementById('residence').selectedIndex].value;
    	params["day"] = document.getElementById('day').options[document.getElementById('day').selectedIndex].value;
        params["month"] = document.getElementById('month').options[document.getElementById('month').selectedIndex].value;
    	params["year"] = document.getElementById('year').options[document.getElementById('year').selectedIndex].value;

    	post_to_url("<?php echo $URL_BASE?>/user/index", params, "post");
	}

	function accept(){
		alert("Reservation: " + machine + " de " + heure +":00 - " + heure1 +":00");
		post(user.php,heure,machine); 
	}

	function logOut(){
		var params = new Array();
		post_to_url("<?php echo $URL_BASE?>/user/delog", params, "post");
	}
</script>

<!-- Fin de código en JavaScript -->

</head>

<body onLoad="setUp();">
</body>

<div id="header">

<div id="selectBox">
<select id= "residence" name="residence" onchange="refreshIndex();">
<option value="A">Residence A</option>
<option value="B">Residence B</option>
<option value="C">Residence C</option>
<option value="D">Residence D</option>
<option value="E">Residence E</option>
<option value="F">Residence F</option>
<option value="G">Residence G</option>
<option value="H">Residence H</option>
<option value="I">Residence I</option>
<option value="J">Residence J</option>
<option value="M">Residence Magellan</option>
</select>
</div>

<div id="selectBox2">
<select id="day" name="day" onchange="refreshIndex();">
<?php 	$day = (int) date("j");
		$month = (int) date("n");
		if($params['month'] == $month){
			for($currDay = $day; $currDay < min(31,15+$day); $currDay++ ){
				echo(sprintf('<option value="%d">%d</option>', $currDay, $currDay));
			}
		} else {
			for($currDay = 1; $currDay < max(15-(30-$day),2); $currDay++ ){
				echo(sprintf('<option value="%d">%d</option>', $currDay, $currDay));
			}
		}
			
?>
</select>
</div>

<div id="selectBox3" ice:editable="*">
  <div align="center">
    <select id="month" name="month" onchange="refreshIndex();">
    <?php 
			$day = (int) date("j");
			$month = (int) date("n");
			$year = (int) date("Y");
			if($params['year']==$year){
				switch($month){
						case 1:
							echo('<option value="1">Janvier</option>');
							break;
						case 2:
							echo('<option value="2">Février</option>');
							break;
						case 3:
							echo('<option value="3">Mars</option>');
							break;
						case 4:
							echo('<option value="4">Avril</option>');
							break;
						case 5:
							echo('<option value="5">Mai</option>');
							break;
						case 6:
							echo('<option value="6">Juin</option>');
							break;
						case 7:
							echo('<option value="7">Juillet</option>');
							break;
						case 8:
							echo('<option value="8">Aout</option>');
							break;
						case 9:
							echo('<option value="9">Setpembre</option>');
							break;
						case 10:
							echo('<option value="10">Octobre</option>');
							break;
						case 11:
							echo('<option value="11">Novembre</option>');
							break;
						case 12:
							echo('<option value="12">Decembre</option>');
							break;
						}
						
				if($day>16){
					switch($month+1){
						case 2:
							echo('<option value="2">Février</option>');
							break;
						case 3:
							echo('<option value="3">Mars</option>');
							break;
						case 4:
							echo('<option value="4">Avril</option>');
							break;
						case 5:
							echo('<option value="5">Mai</option>');
							break;
						case 6:
							echo('<option value="6">Juin</option>');
							break;
						case 7:
							echo('<option value="7">Juillet</option>');
							break;
						case 8:
							echo('<option value="8">Aout</option>');
							break;
						case 9:
							echo('<option value="9">Setpembre</option>');
							break;
						case 10:
							echo('<option value="10">Octobre</option>');
							break;
						case 11:
							echo('<option value="11">Novembre</option>');
							break;
						case 12:
							echo('<option value="12">Decembre</option>');
							break;
						}
				}
			} else {
				echo('<option value="1">Janvier</option>');
			}
    ?>
    </select>
  </div>
</div>
<div id="selectBox4" onchange="refreshIndex();">
  <select id="year" name="year">
  <?php
  	$day = (int) date("j");
	$month = (int) date("n");
  	$year = (int) date("Y");
  	echo(sprintf('<option value="%d">%d</option>',$year,$year));
  	if($month == 12 && $day > 16){
  		echo(sprintf('<option value="%d">%d</option>',$year+1,$year+1));
  	}

   ?>
  </select>
</div>
</div>


<form id="Formulaire" method="post" action="<?php echo $URL_BASE?>/user/login">
	<span id="notLoggedIn">
	    <label for="pseudo">Utilisateur :</label></p>
	   <p>
	     <input name="pseudo" type="text" id="pseudo"/></p>
	   <p>
	     <label for="motdepasse">Mot de passe :</label></p>
	   <p>
	     <input id="motdepasse" name="motdepasse" type="password"/></p>
	   <p>
	     <input type="submit" value="Connexion" />
	   </p>
	</span>
	<span id="loggedIn">
		<p>
		<label for="pseudo"><?php echo($params["username"]); ?></label></p>
		<?php if($params["admin"] == true) :?>
		  <p><a href="<?php echo $URL_BASE ?>/admin/index">Admin Interface</a></p>
		<?php endif; ?>
    <p><a href="<?php echo $URL_BASE ?>/user/showProfile">Profile</a></p>
		<p><a href="#" onclick="logOut();">Log Out</a></p>
		<p><a href="#" onclick="cancelAll();">Cancel all bookings</a></p>
	</span>
 </form>


<div id="tabla"><img src="<?php echo $URL_BASE ?>/static/images/add.gif"> : Réserver ce créneau &nbsp;&nbsp; <img src="<?php echo $URL_BASE ?>/static/images/cancel.gif"> : Annuler une réservation&nbsp;&nbsp;   <img src="<?php echo $URL_BASE ?>/static/images/occupied.gif"> : Créneau occupé&nbsp;&nbsp; <img src="<?php echo $URL_BASE ?>/static/images/repairing.gif"> : En panne&nbsp;&nbsp;  <img src="<?php echo $URL_BASE ?>/static/images/broken.gif"> : Reporter une panne<br /></div>
<p></p>

<div id="links">
<p><a href="<?php echo $URL_BASE ?>/user/rapport">-- Rapport WEB --</a></p>
</div>

<?php
//include("app/views/partials/footer.php");
?>

</html>
